<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: allen.qiang
// +----------------------------------------------------------------------

/**
 * 前台公共ajax类
 * @package  ajax
 * @subpackage  shop
 * @author   allen.qiang
 */

class ajax_sys {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && is_numeric($_REQUEST['operFlg'])) {

            switch($_REQUEST['operFlg']) {

                case 1: //发送手机短信
                    $this->sendSms();
                    break;

                default:
                    returnJsonData('参数错误');
                    break;

            }

        } else {
            returnJsonData('参数错误');
        }

    }

    /**
     * 发送手机短信
     */
    private function sendSms() {

        global $G_SHOP;

        //加载短信类
        quanli_include('module/base/smsHandler.class.php');

        //加载字段类
        quanli_include('module/base/itemHandler.class.php');

        //实例化公共类
        $commHandler =new commHandler( $G_SHOP->DBCA );

        //实例化会员类
        $customerHandler = new customerHandler( $G_SHOP->DBCA );

        //实例化短信类
        $smsHandler = new smsHandler( $G_SHOP->DBCA );

        //实例化字典类
        $itemHandler = new itemHandler( $G_SHOP->DBCA );

        if (!isset($_REQUEST['mobile'],$_REQUEST['optionType']) || !$commHandler->isMobile($_REQUEST['mobile']) || !$commHandler->isInteger($_REQUEST['optionType'])) {
            $this->toReturn('参数错误');
        }

        $mobile = $_REQUEST['mobile']; //手机号码
        $optionType = $_REQUEST['optionType']; //操作类型

        $current_time = time(); //当前时间戳

        $last_send_time = $G_SHOP->sessionCache_get('dxyzm_time_' . $mobile); //上一次发放短信的时间戳

        if (!empty($last_send_time) && $current_time - $last_send_time < 60) {
            $this->toReturn('手机短信已发送，请一分钟以后再试');
        }

        /**
         * ============================
         * 根据手机号查询会员信息
         * ============================
         */
        $arg_array = array(
            'custID' => $mobile
        );
        $cust_array = $customerHandler->getCustomerInfo( $arg_array );
        if ($cust_array) {
    		$this->toReturn('手机号码已被注册', 404);
        }
        /**
         * ============================
         * 根据手机号查询会员信息
         * ============================
         */

        /**
         * ============================
         * 发送短信
         * ============================
         */

        //生成短信验证码
        $yzm = $smsHandler->createSmsCode();

        //查询短信内容
        $arg_array = array(
            'itemCategoryID' => 11,
            'extend' => ' AND extend_2 = "reg_sms"'
        );
        $item_array = $itemHandler->searchItem( $arg_array );
        
        if (!$item_array) {
            $this->toReturn('短信内容为空，发送失败', 404);
        }
        $sms_conent = str_replace('$yzm', $yzm, $item_array[0]['extend_1']);
		
        //发送短信
        $result_array = $smsHandler->sendSMS( $mobile, $sms_conent );
		
        /**
         * ============================
         * 发送短信
         * ============================
         */

        if ($result_array['code'] == 200) {

            //保存短信验证码到redis
            $G_SHOP->sessionCache_set('dxyzm_' . $mobile, md5($yzm), 3600);//将验证码信息保存到memcache中去
            $G_SHOP->sessionCache_set('dxyzm_time_' . $mobile, time(), 3600);

        }

        $this->toReturn($result_array['message'], $result_array['code']);

    }

}